/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.util.ArrayList;
import javax.ejb.Stateless;
import javax.inject.Named;

/**
 *
 * @author Mattia Gianotti
 */
@Named ("searchBean")
@Stateless
public class SearchBean {

    /**
     * Variables of the class
     * NUMBEROFVISUALIZEDRESULT : number of eleemnt that will be shown in the page
     * search : is the string that the user will type in the inputtext and that will be used to filter the research
     * base : indicate the first eleemnt that is shown in the page. it start from 0 and can increment or decrement of NUMBEROFVISUALIZEDRESULT not exceeding the dimension of searched
     * searched : result of the query
     */
    private final int NUMBEROFVISUALIZEDRESULT = 5;
    private String search;
    private int base = 0;
    private ArrayList <String> searched;

    /**
     * Method that return the whole result of the query
     * @return the whole result of the query in an ArrayList form
     */
    public ArrayList<String> getSearched() {
        return searched;
    }
    /**
     * Method to set the whole result of the query
     * @param searched ArrayList of result
     */
    public void setSearched(ArrayList<String> searched) {
        this.searched = searched;
    }
    
    /**
     * empty constructor
     */
    public SearchBean() {
        searched = new ArrayList<>();
    }
    
    /**
     * base constructor
     * @param search
     */
    public SearchBean(String search) {
        this.search = search;
        searched = new ArrayList<>();
    }
    
    /**
     * Method to get the value of the filter of the research
     * @return searched filter
     */
    public String getSearch() {
        return search;
    }
    
    /**
     * Set the value of the search variable
     * @param search 
     */
    public void setSearch(String search) {
        System.out.println("settato search a " + search);
        this.search = search;
    }
    
    /**
     * return the value of the base
     * @return int
     */
    public int getBase() {
        return base;
    }
    
    /**
     * set the value of the base
     * @param base 
     */
    public void setBase(int base) {
        this.base = base;
    }
    
    /**
     * increase the value of the base of NUMBEROFVISUALIZEDRESULT to visualize the following set of result
     */
    public void next() {
        System.out.println(base);
        if(base < searched.size()-NUMBEROFVISUALIZEDRESULT){
            base = base + NUMBEROFVISUALIZEDRESULT;
        }
    }
    
    /**
     * decrease the value of the base of NUMBEROFVISUALIZEDRESULT to visualize the previous set of result
     */
    public void prev() {
        System.out.print(base);
        if(base > 0){
            base = base - NUMBEROFVISUALIZEDRESULT;
        }
    }
    
    /**
     * Function to start the search of result to the query
     */
    public void lookForTheInput(){
        System.out.println("iniziata ricerca");
        searched.add("A");
        base = 0;
        searched.stream().forEach((s) -> {
            System.out.println(s);
        });
    }
    
    /**
     * Method to update the name of the results shown in the page
     * @param i offset starting from the base of the required name
     * @return the name
     */
    public String name1(int i){
        System.out.println("name letto" + (i));
        if(searched.size()>0 && (searched.size()-1) >= (i + base)){
            System.out.println("searched.size()"  + searched.size());
            return(searched.get(i + base) + "name");
        }else{
            return("");
        }
    }
    
    /**
     * Method to update the surname of the results shown in the page
     * @param i offset starting from the base of the required surname
     * @return the surname
     */
    public String surname1(int i){
        System.out.println("surname letto"  + (i));
        if(searched.size()>0 && (searched.size()-1) >= (i + base)){
            System.out.println("searched.size()"  + searched.size());
            return(searched.get(i + base) + "surname");
        }else{
            return("");
        }
    }
    
    /**
     * Method to update the email of the results shown in the page
     * @param i offset starting from the base of the required email
     * @return the email
     */
    public String email1(int i){
        System.out.println("email letto" + (i));
        if(searched.size()>0 && (searched.size()-1) >= (i + base)){
            return(searched.get(i + base) + "email");
        }else{
            return("");
        }
    }
}

